package com.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.bussiness.domain.Poi;
import com.bussiness.domain.PoiTemplate;
import com.dao.PoiDao;

public class PoiDaoImpl implements PoiDao {
	private static Logger logger = Logger.getLogger(PoiDaoImpl.class);   
	private Session session;
	private SessionFactory sessionFactory;
	
	public PoiDaoImpl() {
		super();
		sessionFactory = HibernateUtil.buildSessionFactory();
	}

	@Override
	public Long createPoi(Poi poi) {
		session = HibernateUtil.openSession();
		session.beginTransaction();
		Long i = (Long) session.save(poi);
		session.getTransaction().commit();
		session.close();
		if (i!=null){ 
			System.out.println(i);
			return i;
		}else return null;
	}

	@Override
	public Poi readPoi(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean deletePoi(Poi poi) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updatePoi(Poi poi) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Long createPoiTemplate(PoiTemplate temp) {		
		//Hibernate save process
		session = HibernateUtil.openSession();
		session.beginTransaction();
		Long i = (Long) session.save(temp);
		session.getTransaction().commit();
		session.close();
		if (i!=null){ 
			logger.info(i);
			return i;
		}else {
			logger.info("Save of Poi Template failed");
			return null;
		}

	}

	@Override
	public List<PoiTemplate> readAllPoiTemplates() {
		session = HibernateUtil.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from PoiTemplate");
		List result = q.list();
		for ( PoiTemplate temp : (List<PoiTemplate>) result ) {
		    logger.info(temp);
		}
		session.getTransaction().commit();
		session.close();
		return result;
	}
	
	@Override
	public PoiTemplate readPoiTemplate(Long id) {
		session = HibernateUtil.openSession();
//		session.beginTransaction();
		PoiTemplate result = (PoiTemplate) session.load(PoiTemplate.class, id);
/*		session.flush();
		session.getTransaction().commit();*/
		//HibernateUtil.close(session);
		return result;
	}

}
